﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public static class RegexHelper
    {
        private const string WPFCOLOR = @"^#?([a-f]|[A-F]|[0-9]){4}(([a-f]|[A-F]|[0-9]){4})?$";
        private const string HTMLCOLOR = @"^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$";
        private const string NUMBER = @"^[0-9]+$";
        private const string ENGLISH = @"^[A-Za-z0-9]+$";
        private const string EMAIL = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
        private const string IP = @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$";
        private const string URL = @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$";
        private const string CHINESE = @"^[\u4e00-\u9fa5]{2,}$";
        private const RegexOptions OPTIONS = RegexOptions.IgnoreCase | RegexOptions.Compiled;

        /// <summary>
        /// 判断是否网页颜色 如:#ffffffff 包含透明度
        /// </summary>
        /// <param name="strColor"></param>
        /// <returns></returns>
        public static bool IsWpfColor(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(WPFCOLOR, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否网页颜色 如:#ffffff 不包含透明度
        /// </summary>
        /// <param name="strColor"></param>
        /// <returns></returns>
        public static bool IsHtmlColor(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(HTMLCOLOR, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否为数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumber(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(NUMBER, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否为电子邮件
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(EMAIL, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否为IP地址
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIP(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(IP, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否为URL
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUrl(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(URL, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否为中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsChinese(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(CHINESE, OPTIONS);
            return r.IsMatch(str);
        }
        /// <summary>
        /// 判断是否为英文字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsAbc(string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            Regex r = new Regex(ENGLISH, OPTIONS);
            return r.IsMatch(str);
        }
    }
}
